package com.zhangxi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhangxi.constant.StatusConstant;
import com.zhangxi.context.BaseContext;
import com.zhangxi.dto.CategoryDTO;
import com.zhangxi.dto.CategoryPageQueryDTO;
import com.zhangxi.entity.Category;
import com.zhangxi.mapper.CategoryMapper;
import com.zhangxi.result.PageResult;
import com.zhangxi.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhangxi
 * @create 2024-04-21 14:04
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        //定义分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        //执行查询操作
        Page<Category> categories = categoryMapper.pageQuery(categoryPageQueryDTO);
        //封装结果
        PageResult  pageResult = new PageResult();
        pageResult.setTotal(categories.getTotal());
        pageResult.setRecords(categories.getResult());
        return pageResult;
    }

    /**
     * 修改分类状态
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                //修改时间和修改人的修改
//                .updateTime(LocalDateTime.now())
//                .updateUser(BaseContext.getCurrentId())
                //已新增Aspect对公共字段的填充
                .build();
        categoryMapper.update(category);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void insert(CategoryDTO categoryDTO) {

        Category category =new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO,category);
        //设置各字段的默认值
        category.setStatus(StatusConstant.ENABLE);
//        category.setCreateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        //已新增Aspect处理公共字段
        categoryMapper.insert(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void delete(Long id) {
        categoryMapper.delete(id);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category =new Category();
        //对象属性拷贝
        BeanUtils.copyProperties(categoryDTO,category);
        //设置各字段的默认值
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
//        已新增Aspect填充公共字段
        categoryMapper.update(category);
    }

    /**
     * 根据分类类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getCategoriesByType(Integer type) {
        return categoryMapper.getCategoriesByType(type);
    }
}
